/// -----------------------------------------------------------------------------------------------------------
/// Module      :  SalesForceWCFAdapter.cs
/// Description :  The main adapter class which inherits from Adapter
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.ServiceModel.Description;
using System.Windows.Forms;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace WCFLOBTalk.SalesForceWCF
{
    public class SalesForceWCFAdapter : Adapter
    {
        // Scheme associated with the adapter
        internal const string SCHEME = "SalesForceWCF";
        // Namespace for the proxy that will be generated from the adapter schema
        internal const string SERVICENAMESPACE = "salesforcewcf://wcflobtalk.salesforcewcf";
        // Initializes the AdapterEnvironmentSettings class
        private static AdapterEnvironmentSettings environmentSettings = new AdapterEnvironmentSettings();

        #region Custom Generated Fields

        private string _Host = "";
        private HostTypeEnum _HostType = HostTypeEnum.u;
        private string _Version = "";
        private ProtocolTypesEnum _Protocol = ProtocolTypesEnum.SOAP;
        private string _SecurityToken = "";
        private string _Username = "";
        private string _Password = "";
        

        #endregion Custom Generated Fields

        #region  Constructor

        /// <summary>
        /// Initializes a new instance of the SalesForceWCFAdapter class
        /// </summary>
        public SalesForceWCFAdapter()
            : base(environmentSettings)
        {
            Settings.Metadata.DefaultMetadataNamespace = SERVICENAMESPACE;

            this.Settings.Metadata.GenerateWsdlDocumentation = true;

            //MessageBox.Show("Creating adapter instance.");
        }

        /// <summary>
        /// Initializes a new instance of the SalesForceWCFAdapter class with a binding
        /// </summary>
        public SalesForceWCFAdapter(SalesForceWCFAdapter binding)
            : base(binding)
        {
            //MessageBox.Show("Creating adapter instance.");

            this.Host = binding.Host;
            this.HostType = binding.HostType;
            this.Protocol = binding.Protocol;
            this.SecurityToken = binding.SecurityToken;
            this.Version = binding.Version;
            this.Username = binding.Username;
            this.Password = binding.Password;

            this.Settings.Metadata.GenerateWsdlDocumentation = true;
        }

        #endregion Constructor

        #region Custom Generated Properties

        [ConfigurationProperty("host", DefaultValue = "test.salesforce.com")]
        public string Host
        {
            get
            {
                return this._Host;
            }
            set
            {
                this._Host = value;
            }
        }

        [ConfigurationProperty("hostType", DefaultValue = HostTypeEnum.u)]
        public HostTypeEnum HostType
        {
            get
            {
                return this._HostType;
            }
            set
            {
                this._HostType = value;
            }
        }

        [ConfigurationProperty("version", DefaultValue = "8.0")]
        public string Version
        {
            get
            {
                return this._Version;
            }
            set
            {
                this._Version = value;
            }
        }

        [ConfigurationProperty("protocol", DefaultValue = ProtocolTypesEnum.SOAP)]
        public ProtocolTypesEnum Protocol
        {
            get
            {
                return this._Protocol;
            }
            set
            {
                this._Protocol = value;
            }
        }

        [ConfigurationProperty("securityToken", DefaultValue = "")]
        public string SecurityToken
        {
            get
            {
                return this._SecurityToken;
            }
            set
            {
                this._SecurityToken = value;
            }
        }

        [ConfigurationProperty("username", DefaultValue = "")]
        public string Username
        {
            get
            {
                return this._Username;
            }
            set
            {
                this._Username = value;
            }
        }

        [ConfigurationProperty("password", DefaultValue = "")]
        public string Password
        {
            get
            {
                return this._Password;
            }
            set
            {
                this._Password = value;
            }
        }



        #endregion Custom Generated Properties

        #region Public Properties

        /// <summary>
        /// Gets the URI transport scheme that is used by the adapter
        /// </summary>
        public override string Scheme
        {
            get
            {
                return SCHEME;
            }
        }

        #endregion Public Properties

        #region Protected Methods

        /// <summary>
        /// Creates a ConnectionUri instance from the provided Uri
        /// </summary>
        protected override ConnectionUri BuildConnectionUri(Uri uri)
        {
            //MessageBox.Show("Creating new sfconnectionuri instance.");
            //MessageBox.Show("passed in uri: " + uri.ToString());

            return new SalesForceWCFAdapterConnectionUri(uri);
        }

        /// <summary>
        /// Builds a connection factory from the ConnectionUri and ClientCredentials
        /// </summary>
        protected override IConnectionFactory BuildConnectionFactory(
            ConnectionUri connectionUri
            , ClientCredentials clientCredentials
            , System.ServiceModel.Channels.BindingContext context)
        {
            return new SalesForceWCFAdapterConnectionFactory(connectionUri, clientCredentials, this);
        }

        /// <summary>
        /// Returns a clone of the adapter object
        /// </summary>
        protected override Adapter CloneAdapter()
        {
            return new SalesForceWCFAdapter(this);
        }

        /// <summary>
        /// Indicates whether the provided TConnectionHandler is supported by the adapter or not
        /// </summary>
        protected override bool IsHandlerSupported<TConnectionHandler>()
        {
            return (
                  typeof(IAsyncOutboundHandler) == typeof(TConnectionHandler)
                || typeof(IOutboundHandler) == typeof(TConnectionHandler)
                || typeof(IAsyncInboundHandler) == typeof(TConnectionHandler)
                || typeof(IInboundHandler) == typeof(TConnectionHandler)
                || typeof(IMetadataResolverHandler) == typeof(TConnectionHandler)
                || typeof(IMetadataBrowseHandler) == typeof(TConnectionHandler)
                || typeof(IMetadataSearchHandler) == typeof(TConnectionHandler));
        }

        /// <summary>
        /// Gets the namespace that is used when generating schema and WSDL
        /// </summary>
        protected override string Namespace
        {
            get
            {
                return SERVICENAMESPACE;
            }
        }

        #endregion Protected Methods
    }
}
